function loadUserDatabase(){

	var xhttp=false;
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else // for IE 5/6
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
//    var country=document.getElementById('Field102').value;
    xhttp.open("POST","/fetchAllUsersServlet",true);
	xhttp.send();
	xhttp.onreadystatechange=handleServerResponseForLoadUser(xhttp);

}


function handleServerResponseForLoadUser(xmlHttpRequest) {
	
    	return function(){
		if (xmlHttpRequest.readyState == 4) {
		
			if (xmlHttpRequest.status == 200) {

              // Var is a big string with ,country1,country2,country3...etc
				// Want to seperate into 3 variables with var1 = country1, var2 = country2, var3 = country3			
				var userString = xmlHttpRequest.responseText;
				if (userString){
//					alert ("userString has value");
					breakupString(userString,'Users' );
				}
				else {
//					alert ("userString is empty");
					deleteRows('Users');
					addRow('No Active Users in Database', 'Users');
				}
              }
			else {
				alert("Error during AJAX call. Please try again");
			}
		}
	}

}



function breakupString (breakString, idString){
	
	//word[0] contains "Welcome", word[1] contains "to" etc
	var brokenString = breakString.split(",");
	for (i = 1; i < brokenString.length; i++)
	{	
		addRow(brokenString[i],idString);
	}
		

}

//////////////////////////////////////////////////////////////////////////
// addRow is a javascript function that adds a row to an existing table
// This version adds a row of text, with value equal to string, to a 
// table with tableID
//////////////////////////////////////////////////////////////////////////

function addRow(string, tableID)
{
	
  	// First delete all rows of the previous table.
//  	deleteRows(tableID);
	
  	var tbody = document.getElementById(tableID).getElementsByTagName("tbody")[0];
  	var row = document.createElement("TR");

  	var cell2 = document.createElement("TD");
  	cell2.innerHTML = string;
  	row.appendChild(cell2);
  	tbody.appendChild(row);
  	//alert(row.innerHTML);
}

///////////////////////////////////////////////////////////
// This function calls the servlet generateUsersServlet
///////////////////////////////////////////////////////////
function generateUsers(){

	var xhttp=false;
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else // for IE 5/6
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
//    var country=document.getElementById('Field102').value;
    xhttp.open("POST","/generateUsersServlet",true);
	xhttp.send();
//	xhttp.onreadystatechange=handleServerResponseForLoadUser(xhttp);

}

/////////////////////////////////////////////////////////////////////
// This function calls the Servlet deleteAllUsersServlet
////////////////////////////////////////////////////////////////////
function deleteUserDatabase()
{

	var xhttp=false;
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else // for IE 5/6
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

    xhttp.open("POST","/deleteAllUsersServlet",true);
	xhttp.send();
	deleteAllRows('Users');
}

/////////////////////////////////////////////////////////////////////////
// This function deletes all rows of the table
/////////////////////////////////////////////////////////////////////////

function deleteAllRows(tableID){
  	var tbody = document.getElementById(tableID).getElementsByTagName("tbody")[0];
  	alert("in Delete all rows");
  	for(var i=tbody.rows.length;i>0;i--) {
  		document.getElementById(tableID).deleteRow(i-1);
  	} 
}